<?php
 class Madmin extends CI_Model{
	function __construct(){
		parent::__construct();
	}
	//管理员的一些操作
	function get_manage($page,$page_size){
		$sql = "SELECT * FROM manage ORDER BY id DESC LIMIT $page,$page_size";
		$query = $this->db->query($sql);
		return $query->result();
	}
	//这个函数是为了修改之类的操作时候用的,只获取一条数据
	function get_one_manage($id){
		$sql = "SELECT * FROM manage WHERE id=$id";
		$query = $this->db->query($sql);
		return $query->result();	
	}
	//查询数据的总函数，分页使用
	function get_count_manage(){
		$sql = "SELECT COUNT(*) as count FROM manage";
		$query = $this->db->query($sql);
		return $query->result();
	}
	function add_manage(){
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		$password = sha1($password);
		$notpassword = $this->input->post('notpassword');
		$notpassword = sha1($notpassword);
		$level = $this->input->post('level');
		$last_ip = $_SERVER['REMOTE_ADDR'];
		$sql = "INSERT INTO manage(id,username,password,level,last_ip,last_time,login_count)
		VALUES('','$username','$password','$level','$last_ip',NOW(),0)";
		$this->db->query($sql);
		return $this->db->affected_rows();
	}
	
	function update_manage(){
		$id = $this->input->post('id');
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		$password = sha1($password);
		$level = $this->input->post('level');
		$sql = "UPDATE manage SET username='$username',password='$password',level='$level' WHERE id=$id LIMIT 1";
		$this->db->query($sql);
		return $this->db->affected_rows();
				
	}
	
	function delete_manage($id){
		$sql = "DELETE FROM manage WHERE id = $id LIMIT 1";
		$this->db->query($sql);
		return $this->db->affected_rows();
	}
	
	
	
	
	//导航的一系列操作
	//导航按理来说，应该不需要分页的，因为导航的数量不会太多，如果需要，到时候再加上
	function get_nav(){
		$sql = "SELECT * FROM nav ORDER BY id ASC";
		$query = $this->db->query($sql);
		return $query->result();
	}
	
	function get_all_parent_nav(){
		$sql = "SELECT * FROM nav  WHERE pid = 0 ORDER BY id ASC";
		$query = $this->db->query($sql);
		return $query->result();
	}
	
	function get_all_child_nav($id){
		
		$sql = "SELECT * FROM nav  WHERE pid = $id ORDER BY id ASC";
		$query = $this->db->query($sql);
		return $query->result();
	}


	//查询子类all
	function get_child_nav($id){
		$sql = "SELECT * FROM nav WHERE pid=$id ORDER BY ID ASC";
		$query = $this->db->query($sql);
		return $query->result();
	}
	
	//这个函数是为了修改之类的操作时候用的,只获取一条数据
	function get_one_nav($id){
		$sql = "SELECT * FROM nav WHERE id=$id";
		$query = $this->db->query($sql);
		return $query->result();
	}
	function delete_nav($id){
		$sql = "DELETE FROM nav WHERE id = $id LIMIT 1";
		$this->db->query($sql);
		return $this->db->affected_rows();
	}
	//增加导航功能 实现
	function add_nav(){
		$nav_name = $this->input->post('nav_name');
		$nav_info = $this->input->post('nav_info');
		$sql = "INSERT INTO nav(id,nav_name,nav_info,pid)
		VALUES('','$nav_name','$nav_info',0)";
		$this->db->query($sql);
		return $this->db->affected_rows();
	}
	
	//增加子导航的实现
	function add_nav_child(){
		$id = $this->input->post('id');
		$nav_name = $this->input->post('nav_name');
		$nav_info = $this->input->post('nav_info');
		$sql = "INSERT INTO nav(id,nav_name,nav_info,pid)
		VALUES('','$nav_name','$nav_info','$id')";
		$this->db->query($sql);
		return $this->db->affected_rows();
	}
	//update功能的实现
	function update_nav(){
		$id = $this->input->post('id');
		$nav_name = $this->input->post('nav_name');
		$nav_info = $this->input->post('nav_info');
		$sql = "UPDATE nav SET nav_name='$nav_name',nav_info='$nav_info' WHERE id=$id LIMIT 1";
		$this->db->query($sql);
		return $this->db->affected_rows();
	
	}
	
	function update_nav_child(){
		$id = $this->input->post('id');
		$nav_name = $this->input->post('nav_name');
		$nav_info = $this->input->post('nav_info');
		$sql = "UPDATE nav SET nav_name='$nav_name',nav_info='$nav_info' WHERE id=$id LIMIT 1";
		$this->db->query($sql);
		return $this->db->affected_rows();
	}
	
	
	//删除子类导航
	function delete_child_nav($id){
		$sql = "DELETE FROM nav WHERE id = $id LIMIT 1";
		$this->db->query($sql);
		return $this->db->affected_rows();
	}
	
	//添加文章
	function add_content(){
			$title = $this->input->post('title');
			$nav = $this->input->post('nav');
			$tag = $this->input->post('tag');
			$keyword = $this->input->post('keyword');
			$source = $this->input->post('source');
			$author = $this->input->post('author');
			$info = $this->input->post('info');
			$content = $this->input->post('text');
		$sql = "INSERT INTO content(id,title,nav,tag,keyword,source,author,info,content,count,date
		)VALUES('','$title','$nav','$tag','$keyword','$source','$author','$info','$content',0,NOW())";
		$this->db->query($sql);
		return $this->db->affected_rows();
		}
	//查询所有的文章
	function get_content(){
			$sql = "SELECT * FROM content";
			$query = $this->db->query($sql);
			return $query->result();
		}
	
	
	
}